<template>
	<view class="container" style="padding-bottom: 120rpx;">
		<u--form ref="queryForm" :model="query">
			<u-form-item label="" prop="materialCode" style="display: flex;">
				<span style="margin-right: 20rpx;">物料码</span>
				<u--input 
					placeholder="请输入/扫描" 
					v-model="query.materialCode" 
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="" prop="materialBatch" style="display: flex;">
				<span style="margin-right: 20rpx;">批次号</span>
				<u--input 
					placeholder="请输入/扫描" 
					v-model="query.materialBatch" 
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="" prop="destination" style="display: flex;">
				<span style="margin-right: 50rpx;">数量</span>
				<u--input
					type="number"
					placeholder="请输入/扫描" 
					v-model="query.qty" 
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
		</u--form>
		<uni-table ref="table" border stripe emptyText="暂无物料" style="margin: 10px 0;height: auto;">
			<uni-tr>
				<uni-th width="30">物料码</uni-th>
				<uni-th width="30">批次号</uni-th>
				<uni-th width="20">数量</uni-th>
				<uni-th width="20">操作</uni-th>
			</uni-tr>
			<uni-tr v-for="(item, index) in list" :key="index">
				<uni-td>
					<view class="selectable" @click="checkDetail(item.materialCode)">
						{{ item.materialCode.slice(0, 9) }}</br>{{ item.materialCode.slice(9, 18) }}
					</view>
				</uni-td>
				<uni-td class="selectable">{{ item.materialBatch }}</uni-td>
				<uni-td class="selectable">{{ item.qty }}</uni-td>
				<uni-td><u-button  style="padding: 10px;font-size: 18rpx;" size="medium" type="error" @click="deleteMaterial(item)">删 除</u-button></uni-td>
			</uni-tr>
		</uni-table>
		<!-- 模态框 -->
		<u-modal
			:show="show"
			:title="title"
			@confirm="cancelModal"
		>
			<view style="display: flex;flex-direction: column;">
				<view style="display: flex;">
					<view style="flex: 2;">物料编号：</view>
					<view style="flex: 3;">{{ selectedMaterial.materialCode }}</view>
				</view>
				<view style="display: flex;">
					<view style="flex: 2;">物料名称：</view>
					<view style="flex: 3;">{{ selectedMaterial.materialName }}</view>
				</view>
				<view style="display: flex;">
					<view style="flex: 2;">物料批次号：</view>
					<view style="flex: 3;">{{ selectedMaterial.materialBatch }}</view>
				</view>
				<view style="display: flex;">
					<view style="flex: 2;">数量：</view>
					<view style="flex: 3;">{{ selectedMaterial.materialQty }}</view>
				</view>
				<view style="display: flex;">
					<view style="flex: 2;">单位：</view>
					<view style="flex: 3;">{{ selectedMaterial.unit }}</view>
				</view>
			</view>
		</u-modal>
		<view style="position: fixed; bottom: 0; left: 0; width: 100%; padding: 10rpx; background-color: #fff; display: flex; justify-content: space-between;box-sizing: border-box;gap: 20rpx;">
			<u-button type="primary" text="添加" @click="addMaterial"></u-button>
			<u-button type="primary" text="确认叫车" @click="submit"></u-button>
			<u-button type="warning" text="重置" @click="reset"></u-button>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		uniCard,
		uniTable,
		uniTr,
		uniTd,
		uniTh
	} from '@dcloudio/uni-ui'
	import { callSuitablePalletAPI } from '@/api/work/callCar.js'
	export default {
		components: {
			uniCard,
			uniTable,
			uniTr,
			uniTd,
			uniTh
		},
		data() {
			return {
				query: {
					materialCode: '',
					materialBatch: '',
					qty: ''
				},
				list: [],
				show: false,
				title: '物料详情',
				selectedMaterial: {}
			};
		},
		onLoad(options) {
			uni.$on('scancodedate',(data) => this.getCode(data.code))
		},
		onUnload() {  
		  // 移除监听事件      
		  uni.$off('scancodedate')
		},
		methods: {
			getCode(code) {
				if (code.length === 18) {
					this.query.materialCode = code
				} else if (code.length > 0) {
					this.query.materialBatch = code
				}
			},
			checkDetail(val) {
				const foundMaterial = this.list.find(item => item.materialCode === val)
				if (foundMaterial) {
					this.selectedMaterial = foundMaterial
					this.show = true
				}
			},
			cancelModal() {
				// this.selectedMaterial = {}
				this.show = false
			},
			addMaterial() {
				if (!this.query.materialCode) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请输入/扫描物料码'
					})
				}
				if (this.query.materialCode && !this.query.materialBatch) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请输入/扫描批次号'
					})
				}
				if (this.query.materialCode && this.query.materialBatch && !this.query.qty) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请输入数量'
					})
				}
				if (!/^\d+$/.test(this.query.qty)) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '数量必须为数字且大于等于0'
					})
				}
				this.list.push(this.query)
				this.query = {
					materialCode: '',
					materialBatch: '',
					qty: ''
				}
			},
			deleteMaterial(item) {
				const index = this.list.indexOf(item)
				if (index > -1) {
					this.list.splice(index, 1)
				}
			},
			async submit() {
				const res = await callSuitablePalletAPI(this.list)
				if (res.meta.code === 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: res.meta.msg
					})
					this.reset()
				} else {
					this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			},
			reset() {
				this.list = []
				this.query = {
					materialCode: '',
					materialBatch: '',
					qty: ''
				}
			}
		}
	}
</script>

<style lang="scss">
.selectable {
	user-select: text; /* 确保内容可选 */
}
</style>
